<template>
  <div class="main">
    <!-- 头部组件 -->
    <comp-det-heater>
      <template v-slot:left>
        <!-- <router-link to="/home/mine"> -->
          <div class="left" @click="$router.go(-1)"><van-icon name="arrow-left" /></div>
        <!-- </router-link> -->
      </template>
      <template v-slot:center>
        <span>编辑资料</span>
      </template>
    </comp-det-heater>
    <!-- 照片头像 -->
    <div class="photoBox">
      <div class="photo">
        <img :src="imgs" alt="" />
        <input
          accept="image/*,audio/*"
          type="file"
          class="file"
          @change="getFiles($event)"
        />
        <div class="camera">
          <div class="iconfont">&#xe601;</div>
        </div>
      </div>
    </div>
    <!-- 个人信息 -->
    <div class="information">
      <div>
        <div>名字</div>
        <input type="text" v-model="name" />
      </div>
      <div>
        <div>账号</div>
        <div>{{ num }}</div>
      </div>
      <div>
        <div>性别</div>
        <van-radio-group v-model="sex" direction="horizontal">
          <van-radio name="男" checked-color="#fa6ba1">男</van-radio>
          <van-radio name="女" checked-color="#fa6ba1">女</van-radio>
        </van-radio-group>
      </div>
      <div>
        <div>生日</div>
        <van-cell class="birthday" :value="date" @click="show = true" />
        <van-calendar v-model="show" @confirm="onConfirm" :min-date="minDate" :max-date="maxDate"/>
      </div>
      <div class="briefIntroduction">
        <el-input
          class="brief"
          type="textarea"
          autosize
          placeholder="个性签名"
          v-model="brief"
        >
        </el-input>
      </div>
    </div>
    <div class="over" @click="propupShow">保存</div>
    <div class="propupBox" v-show="isShow">
      <div class="propup">
        <span>是否保存修改</span>
        <div class="btn">
          <button @click="over">确定</button>
          <button @click="propupShow">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
import CompDetHeater from "../components/comp-detHeater.vue";
export default {
  data() {
    return {
      name: "",
      num: "",
      sex: "",
      brief: "",
      isShow: false,
      imgs: "",
      minDate: new Date(1980, 1, 1),
      maxDate: new Date(2021, 11, 18),
      date: "",
      show: false,
    };
  },
  computed: {
    ...mapState(["mine"]),
  },
  methods: {
    ...mapMutations(['editMine']),
    getFiles(e) {
      var img_ = window.URL.createObjectURL(e.target.files[0]);
      this.imgs = img_;
      console.log(this.imgs);
    },
    formatDate(date) {
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    },
    onConfirm(date) {
      this.show = false;
      this.date = this.formatDate(date);
    },
    over() {
      this.isShow = !this.isShow;
      
      this.$store.commit('editMine',{
        name: this.name,
        imgs: this.imgs,
        num: this.num,
        sex: this.sex,
        date: this.date,
        brief: this.brief

      })
      this.$router.push({
        path: "/home/mine/note",
      });
    },
    propupShow() {
      if( !this.name ){
        this.$message({
          message: '名字不能为空哦~',
          type: 'warning',
        });
        return;
      }
      this.isShow = !this.isShow;
    },
  },
  components: {
    CompDetHeater,
  },
  mounted() {
    this.name = this.mine.name;
    this.num = this.mine.num;
    this.sex = this.mine.sex;
    this.imgs = this.mine.url;
    this.date = this.mine.birthday;
    this.brief = this.mine.brief;
  },
};
</script>
<style scoped lang="scss">
.main {
  margin-top: 60px;
  .photoBox {
    width: 100%;
    height: 200px;
    padding: 50px 0;
    box-sizing: border-box;
    .photo {
      width: 100px;
      height: 100px;
      margin: 0 auto;
      border-radius: 50%;
      border: 2px solid #ccc;
      position: relative;
      img {
        width: 100px;
        height: 100px;
        border-radius: 50%;
      }
      .file {
        position: absolute;
        width: 150px;
        height: 150px;
        top: 10px;
        left: 0;
        opacity: 0;
      }
      .camera {
        position: absolute;
        bottom: 0;
        right: 0;
        background: yellow;
        width: 25px;
        height: 25px;
        border-radius: 50%;
        background: #000;
        color: #fff;
        text-align: center;
        line-height: 25px;
      }
    }
  }
  .information {
    width: 100%;
    padding: 5px 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    > div {
      width: 100%;
      height: 50px;
      line-height: 50px;
      display: flex;
      font-size: 16px;
      color: rgb(145, 145, 145);
      border-bottom: 1px solid #ccc;
      justify-content: space-between;
      input {
        // width: 100px;
        max-width: 100px;
        border: 1px solid transparent;
        text-align: right;
      }
      input:focus {
        outline: none;
      }
      .day {
        max-width: 150px;
        border: 1px solid transparent;
      }
      .birthday{
        width: 80px;
        line-height: 50px;
        padding: 0;
      }
    }
    .briefIntroduction {
      width: 100%;
      min-height: 40px;
      margin-top: 10px;
      box-sizing: border-box;
      border-bottom: 1px solid transparent;
    }
  }
  .over {
    position: fixed;
    bottom: 80px;
    background-color: #fa6ba1;
    color: #fff;
    left: 50%;
    margin-left: -75px;
    width: 150px;
    height: 50px;
    font-size: 20px;
    line-height: 50px;
    text-align: center;
    border-radius: 20px;
    border: 1px solid #ccc;
    box-shadow: 2px 2px 4px #ccc;
  }
  .propupBox {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.2);
    .propup {
      width: 270px;
      height: 170px;
      background: #fff;
      margin: 0 auto;
      margin-top: 300px;
      border-radius: 20px;
      span {
        display: block;
        width: 100%;
        height: 110px;
        line-height: 110px;
        font-size: 22px;
        text-align: center;
        color: rgb(104, 104, 104);
      }
      .btn {
        width: 100%;
        height: 50px;
        display: flex;
        justify-content: space-around;
        button {
          font-size: 12px;
          height: 35px;
          width: 50px;
          border-radius: 5px;
          border: none;
          background-color: #fa6ba1;
          color: #fff;
        }
      }
    }
  }
}
</style>